﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BasePlatformNet;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_DmxConfig : Form
	{
		private Form_MainSet parent = null;			// 父窗体引用

		private BaseCoreNet baseCoreNet = null;		// 底层平台内核引用

		private int devIndex = -1;					// 活跃设备索引
		private int subIndex = 0;					// 分控索引
		private int portIndex = 0;					// 端口索引
		private int miniIndex = -1;					// 灯具索引

		private int currentValue_R = -1;			// 电流参数 - R
		private int currentValue_G = -1;			// 电流参数 - G
		private int currentValue_B = -1;			// 电流参数 - B
		private int currentValue_W = -1;			// 电流参数 - W

		private int targetPower = -1;				// 功率校正目标

		private bool dmxChannelSetting = false;		// 配置通道数操作标识
		private int dmxChannelNum = -1;				// dmx通道数

		private bool innerControlSetting = false;	// 配置内控模式操作标识 
		private int brightness = -1;				// 软件界面亮度值
		private int brightValue = -1;				// 转换后的亮度值
		private int brightnessR = -1;				// 软件界面亮度值 R
		private int brightValueR = -1;				// 转换后的亮度值 R
		private int brightnessG = -1;				// 软件界面亮度值 G
		private int brightValueG = -1;				// 转换后的亮度值 G
		private int brightnessB = -1;				// 软件界面亮度值 B
		private int brightValueB = -1;				// 转换后的亮度值 B
		private int brightnessW = -1;				// 软件界面亮度值 W
		private int brightValueW = -1;				// 转换后的亮度值 W
		private int speed = -1;						// 软件界面速度值
		private int speedValue = -1;				// 转换后的速度值

		private bool pwmSetting = false;			// pwm曲线参数配置标识
		private int pwmContrast = -1;				// 对比度
		private int pwmLevel = -1;				    // 对比级别
		private double pwmGamma = 0.0;				// Gamma值
		private int startGray = -1;					// 起灰灰度
		private int startPWM = -1;					// 起灰PWM
		private int effectLevel = -1;					// 起灰PWM

		private string section = "DmxConfig";		// 多语言文件配置节名称

		private string tip = "提示";					// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_DmxConfig(BaseCoreNet baseCoreNet, Form_MainSet parent)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.baseCoreNet = baseCoreNet;

			this.parent = parent;

			initEnvironment();
		}

		// 初始化运行环境
		private void initEnvironment()
		{
			subIndexTextBox.Text = "1";
			portIndexTextBox.Text = "1";
			miniIndexTextBox.Text = "1";

			// 电流参数
			currentValue_R = 100;
			currentValue_G = 100;
			currentValue_B = 100;
			currentValue_W = 100;
			updateCurrentValue();
			totalCurrentFactorTextBox.Text = "100";

			// 功率校正目标
			targetPower = 0;
			targetPowerTextBox.Text = targetPower.ToString();

			// 内控模式
			innerControlModeComboBox.Items.Add("常亮模式");
			innerControlModeComboBox.Items.Add("跳变模式");
			innerControlModeComboBox.Items.Add("渐变模式");
            innerControlModeComboBox.Items.Add("无自检");
            innerControlModeComboBox.Items.Add("定制模式1");
            innerControlModeComboBox.Items.Add("定制模式2");
			innerControlModeComboBox.SelectedIndex = 0;

			// 无信号设置
			noSignalComboBox.Items.Add("最后一帧");
			noSignalComboBox.Items.Add("内控模式");
			noSignalComboBox.Items.Add("黑灯");
			noSignalComboBox.SelectedIndex = 0;

			// 亮度、速度
			brightness = 100;
			brightnessR = 100;
			brightnessG = 100;
			brightnessB = 100;
			brightnessW = 100;
			brightTextBox.Text = brightness.ToString();
			brightRTextBox.Text = brightness.ToString();
			brightGTextBox.Text = brightness.ToString();
			brightBTextBox.Text = brightness.ToString();
			brightWTextBox.Text = brightness.ToString();
			speed = 100;
			speedTextBox.Text = speed.ToString();

			// 对比度、低灰调整值、Gamma值
			pwmGamma = 2.2;
			startGray = 1;
			startPWM = 1;
			effectLevelComoBox.Items.Add("低有效");
			effectLevelComoBox.Items.Add("高有效");
			effectLevelComoBox.SelectedIndex = 0;
			grayLevelComboBox.Items.Add("65536");
			grayLevelComboBox.Items.Add("32768");
			grayLevelComboBox.Items.Add("16384");
			grayLevelComboBox.Items.Add("8192");
			grayLevelComboBox.Items.Add("4096");
			grayLevelComboBox.Items.Add("2048");
			grayLevelComboBox.Items.Add("1024");
			grayLevelComboBox.Items.Add("512");
			grayLevelComboBox.Items.Add("256");
			grayLevelComboBox.SelectedIndex = 3;
			pwmFreqTextBox.Text = "1920Hz";
			pwmGammaTextBox.Text = pwmGamma.ToString();
			startGrayTextBox.Text = startGray.ToString();
			startPWMTextBox.Text = startPWM.ToString();

			// 通道数，有效电平
			dmxChannelNum = 3;
			dmxChannleTextBox.Text = dmxChannelNum.ToString();

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			innerControlModeComboBox.Items[0] = LanguageOper.ReadValue(section, "inner.Items.0");
			innerControlModeComboBox.Items[1] = LanguageOper.ReadValue(section, "inner.Items.1");
			innerControlModeComboBox.Items[2] = LanguageOper.ReadValue(section, "inner.Items.2");
            innerControlModeComboBox.Items[3] = LanguageOper.ReadValue(section, "inner.Items.3");
            var customStr = LanguageOper.ReadValue(section, "inner.Items.4");
            innerControlModeComboBox.Items[4] = customStr + "1";
            innerControlModeComboBox.Items[5] = customStr + "2";
			noSignalComboBox.Items[0] = LanguageOper.ReadValue(section, "signal.Items.0");
			noSignalComboBox.Items[1] = LanguageOper.ReadValue(section, "signal.Items.1");
			noSignalComboBox.Items[2] = LanguageOper.ReadValue(section, "signal.Items.2");
			effectLevelComoBox.Items[0] = LanguageOper.ReadValue(section, "effect.Items.0");
			effectLevelComoBox.Items[1] = LanguageOper.ReadValue(section, "effect.Items.1");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "dmxConfig.Text");
			subGroupBox.Text = LanguageOper.ReadValue(section, "subGroupBox.Text");
			portGroupBox.Text = LanguageOper.ReadValue(section, "portGroupBox.Text");
			lampGroupBox.Text = LanguageOper.ReadValue(section, "lampGroupBox.Text");
			enableMessageCheckBox.Text = LanguageOper.ReadValue(section, "enableMessageCheckBox.Text");
			channelGroupBox.Text = LanguageOper.ReadValue(section, "channelGroupBox.Text");
			dmxChannelLabel.Text = LanguageOper.ReadValue(section, "dmxChannelLabel.Text");
			saveColorModeButton.Text = LanguageOper.ReadValue(section, "saveColorModeButton.Text");
			inOutMapButton.Text = LanguageOper.ReadValue(section, "inOutMapButton.Text");
			currentGroupBox.Text = LanguageOper.ReadValue(section, "currentGroupBox.Text");
			rLabel.Text = LanguageOper.ReadValue(section, "rLabel.Text");
			gLabel.Text = LanguageOper.ReadValue(section, "gLabel.Text");
			bLabel.Text = LanguageOper.ReadValue(section, "bLabel.Text");
			wLabel.Text = LanguageOper.ReadValue(section, "wLabel.Text");
			currentProButton.Text = LanguageOper.ReadValue(section, "currentProButton.Text");
			setMaxCurrentButton.Text = LanguageOper.ReadValue(section, "setMaxCurrentButton.Text");
			totalLabel.Text = LanguageOper.ReadValue(section, "totalLabel.Text");
			changeByCurrentFactorButton.Text = LanguageOper.ReadValue(section, "changeByCurrentFactorButton.Text");
			readCurrentButton.Text = LanguageOper.ReadValue(section, "readCurrentButton.Text");
			saveCurrentButton.Text = LanguageOper.ReadValue(section, "saveCurrentButton.Text");
			powerGroupBox.Text = LanguageOper.ReadValue(section, "powerGroupBox.Text");
			powerLabel.Text = LanguageOper.ReadValue(section, "powerLabel.Text");
			savePowerButton.Text = LanguageOper.ReadValue(section, "savePowerButton.Text");
			innerGroupBox.Text = LanguageOper.ReadValue(section, "innerGroupBox.Text");
			saveInnerControlButton.Text = LanguageOper.ReadValue(section, "saveInnerControlButton.Text");
			modeLabel.Text = LanguageOper.ReadValue(section, "modeLabel.Text");
			noSignalLabel.Text = LanguageOper.ReadValue(section, "noSignalLabel.Text");
			speedLabel1.Text = LanguageOper.ReadValue(section, "speedLabel1.Text");
			speedLabel2.Text = LanguageOper.ReadValue(section, "speedLabel2.Text");
			speedLabel3.Text = LanguageOper.ReadValue(section, "speedLabel3.Text");
			brightLabel1.Text = LanguageOper.ReadValue(section, "brightLabel1.Text");
			hLabel1.Text = LanguageOper.ReadValue(section, "hLabel1.Text");
			hLabel2.Text = LanguageOper.ReadValue(section, "hLabel2.Text");
			hLabel3.Text = LanguageOper.ReadValue(section, "hLabel3.Text");
			hLabel4.Text = LanguageOper.ReadValue(section, "hLabel4.Text");
			initGroupBox.Text = LanguageOper.ReadValue(section, "initGroupBox.Text");
			dmxSpiListButton.Text = LanguageOper.ReadValue(section, "dmxSpiListButton.Text");
			fullPowerButton.Text = LanguageOper.ReadValue(section, "fullPowerButton.Text");
			rdmGroupBox.Text = LanguageOper.ReadValue(section, "rdmGroupBox.Text");
			//configFileLabel.Text = LanguageOper.ReadValue(section, "configFileLabel.Text");
			//configRdmButton.Text = LanguageOper.ReadValue(section, "configRdmButton.Text");
			//initRdmLogButton.Text = LanguageOper.ReadValue(section, "initRdmLogButton.Text");
			//checkRdmLogButton.Text = LanguageOper.ReadValue(section, "checkRdmLogButton.Text");
			pwmGroupBox.Text = LanguageOper.ReadValue(section, "pwmGroupBox.Text");
			contrastLabel.Text = LanguageOper.ReadValue(section, "contrastLabel.Text");
			pwmFeqLabel.Text = LanguageOper.ReadValue(section, "pwmFeqLabel.Text");
			gammaLabel.Text = LanguageOper.ReadValue(section, "gammaLabel.Text");
			startPwmLabel.Text = LanguageOper.ReadValue(section, "startPwmLabel.Text");
			startGreyLabel.Text = LanguageOper.ReadValue(section, "startGreyLabel.Text");
			effectLevelLabel.Text = LanguageOper.ReadValue(section, "effectLevelLabel.Text");
			savePwmButton.Text = LanguageOper.ReadValue(section, "savePwmButton.Text");
            closeButton.Text = LanguageOper.ReadValue(section, "closeButton.Text");
            TimeLockButton.Text = LanguageOper.ReadValue(section, "TimeLockButton.Text");
            dmxReadCfgButton.Text = LanguageOper.ReadValue(section, "dmxReadCfgButton.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				dmxChannelLabel.Location = new Point(15, 23);
				rLabel.Location = new Point(15, 25);
				gLabel.Location = new Point(15, 52);
				bLabel.Location = new Point(15, 79);
				wLabel.Location = new Point(15, 106);
				totalLabel.Location = new Point(152, 79);
				speedLabel1.Location = new Point(15, 88);
				noSignalLabel.Location = new Point(3, 57);
				brightLabel1.Location = new Point(15, 141);
				contrastLabel.Location = new Point(20, 25);
				gammaLabel.Location = new Point(20, 53);
				startGreyLabel.Location = new Point(8, 81);
				effectLevelLabel.Location = new Point(8, 109);
				startPwmLabel.Location = new Point(135, 53);
			}
			else
			{
				dmxChannelLabel.Location = new Point(5, 23);
				rLabel.Location = new Point(40, 25);
				gLabel.Location = new Point(40, 52);
				bLabel.Location = new Point(40, 79);
				wLabel.Location = new Point(40, 106);
				totalLabel.Location = new Point(150, 79);
				speedLabel1.Location = new Point(27, 88);
				noSignalLabel.Location = new Point(7, 57);
				brightLabel1.Location = new Point(40, 141);
				contrastLabel.Location = new Point(15, 25);
				gammaLabel.Location = new Point(29, 53);
				startGreyLabel.Location = new Point(5, 81);
				effectLevelLabel.Location = new Point(6, 109);
				startPwmLabel.Location = new Point(127, 53);
			}
			if (hLabel1.Text.Equals("null"))
			{
				hLabel1.Text = "";
			}
			if (hLabel2.Text.Equals("null"))
			{
				hLabel2.Text = "";
			}
			if (hLabel3.Text.Equals("null"))
			{
				hLabel3.Text = "";
			}
			if (hLabel4.Text.Equals("null"))
			{
				hLabel4.Text = "";
			}
		}

		// 判断读取电流按钮是否可用
		private void readCurrentButtonEnableOrNot()
		{
			if (allSubCheckBox.Checked == false && allPortCheckBox.Checked == false && allMiniCheckBox.Checked == false)
			{
				readCurrentButton.Enabled = true;
			}
			else
			{
				readCurrentButton.Enabled = false;
			}
		}

        // 判断配置读取按钮是否可用
        private void readCfgButtonEnableOrNot()
        {
            if (allSubCheckBox.Checked == false && allPortCheckBox.Checked == false && allMiniCheckBox.Checked == false)
            {
                dmxReadCfgButton.Enabled = true;
            }
            else
            {
                dmxReadCfgButton.Enabled = false;
            }
        }

		// 将内部电流值更新到 UI 界面
		private void updateCurrentValue()
		{
			currentRTextBox.Text = currentValue_R.ToString();
			currentGTextBox.Text = currentValue_G.ToString();
			currentBTextBox.Text = currentValue_B.ToString();
			currentWTextBox.Text = currentValue_W.ToString();
		}

		// 获取通用参数：活跃设备、分控、端口、灯具
		private bool getCommonParameter()
		{
			// 活跃设备
			devIndex = baseCoreNet.getActiveDevIndex();
			if (devIndex == -1)
			{
				string msg = "当前未检测到分控";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.0");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return false;
			}

			// 分控
			if (allSubCheckBox.Checked)
			{
				subIndex = 0;
			}
			else
			{
				// 检查分控序号
				int tmpSubIndex = -1;
				bool parseRes = int.TryParse(subIndexTextBox.Text.Trim(), out tmpSubIndex);
				if (parseRes == false || tmpSubIndex < 1 || tmpSubIndex > 255)
				{
					string msg = "分控序号解析错误, 应为 [1,255] 之间的整数";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.1");
					}
					
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					return false;
				}
				else
				{
					subIndex = tmpSubIndex;
				}
			}

			// 端口
			if (allPortCheckBox.Checked)
			{
				portIndex = 0;
			}
			else
			{
				// 检查端口序号
				int tmpPortIndex = -1;
				bool parseRes = int.TryParse(portIndexTextBox.Text.Trim(), out tmpPortIndex);
				if (parseRes == false || tmpPortIndex < 1 || tmpPortIndex > 8)
				{
					string msg = "端口序号解析错误, 应为 [1,8] 之间的整数";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.2");
					}
					
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					return false;
				}
				else
				{
					portIndex = tmpPortIndex;
				}
			}

			// 灯具
			if (allMiniCheckBox.Checked)
			{
				miniIndex = -1;
			}
			else
			{
				// 检查灯具序号
				int tmpMiniIndex = -1;
				bool parseRes = int.TryParse(miniIndexTextBox.Text.Trim(), out tmpMiniIndex);
				if (parseRes == false || tmpMiniIndex < 1 || tmpMiniIndex > 65535)
				{
					string msg = "灯具序号解析错误, 应为 [1,65535] 之间的整数";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.3");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					return false;
				}
				else
				{
					miniIndex = tmpMiniIndex;
				}
			}

			return true;
		}

		// 检查特定参数
		private bool checkCurrentPara()
		{
			// 检查电流参数 R
			int tmpCurrentR = -1;
			bool parseResR = int.TryParse(currentRTextBox.Text.Trim(), out tmpCurrentR);
			if (parseResR == false || tmpCurrentR < 0 || tmpCurrentR > 100)
			{
				string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "R");
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "R");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				return false;
			}
			else
			{
				currentValue_R = tmpCurrentR;
			}
			// 检查电流参数 G
			int tmpCurrentG = -1;
			bool parseResG = int.TryParse(currentGTextBox.Text.Trim(), out tmpCurrentG);
			if (parseResG == false || tmpCurrentG < 0 || tmpCurrentG > 100)
			{
				string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "G");
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "G");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				return false;
			}
			else
			{
				currentValue_G = tmpCurrentG;
			}
			// 检查电流参数 B
			int tmpCurrentB = -1;
			bool parseResB = int.TryParse(currentBTextBox.Text.Trim(), out tmpCurrentB);
			if (parseResB == false || tmpCurrentB < 0 || tmpCurrentB > 100)
			{
				string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "B");
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "B");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				return false;
			}
			else
			{
				currentValue_B = tmpCurrentB;
			}
			// 检查电流参数 W
			int tmpCurrentW = -1;
			bool parseResW = int.TryParse(currentWTextBox.Text.Trim(), out tmpCurrentW);
			if (parseResW == false || tmpCurrentW < 0 || tmpCurrentW > 100)
			{
				string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "W");
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "W");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				return false;
			}
			else
			{
				currentValue_W = tmpCurrentW;
			}

			return true;
		}

		/******************************************************************************/

		// 分控 - 全局/定点
		private void allSubCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			subLabel.Enabled = !allSubCheckBox.Checked;
			subIndexTextBox.Enabled = !allSubCheckBox.Checked;

			readCurrentButtonEnableOrNot();
            readCfgButtonEnableOrNot();
		}

		// 端口 - 全局/定点
		private void allPortCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			portLabel.Enabled = !allPortCheckBox.Checked;
			portIndexTextBox.Enabled = !allPortCheckBox.Checked;

			readCurrentButtonEnableOrNot();
            readCfgButtonEnableOrNot();
		}

		// 灯具 - 全局/定点
		private void allMiniCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			miniLabel.Enabled = !allMiniCheckBox.Checked;
			miniIndexTextBox.Enabled = !allMiniCheckBox.Checked;

			readCurrentButtonEnableOrNot();
            readCfgButtonEnableOrNot();
		}

		// 功率校正
		private void savePowerButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 检查校正目标
			int tmpTargetPower = -1;
			bool parseRes = int.TryParse(targetPowerTextBox.Text.Trim(), out tmpTargetPower);
			if (parseRes == false || tmpTargetPower < 0 || tmpTargetPower > 1000)
			{
				string msg = "功率校正目标解析错误, 应为 [0,1000] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}
			else
			{
				targetPower = tmpTargetPower;
			}

			byte[] cmdData = new byte[256];

			cmdData[2] = 0x69;

			byte[] targetPowerBytes = Tools.int2Byte2BigEnd(targetPower);
			cmdData[8] = targetPowerBytes[0];
			cmdData[9] = targetPowerBytes[1];

			baseCoreNet.sendMiniCmd(devIndex, subIndex, portIndex, miniIndex, cmdData, 14);

			if (enableMessageCheckBox.Checked)
			{
				string msg = "功率校正目标保存完成";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.6");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		// 电流配置 - 普通 - 读取
		private void readCurrentButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}
			
			byte[] cmdData = new byte[256];
			cmdData[2] = 0x70;
			int cmdCommNum = baseCoreNet.sendMiniCmdWithCheck(devIndex, subIndex, portIndex, miniIndex, cmdData, 32);

			byte[] ackData = new byte[256];
			bool success = baseCoreNet.getMiniAck(devIndex, subIndex, portIndex, ackData, cmdCommNum);
			if (success == true)
			{
				currentValue_R = ackData[8];
				currentValue_G = ackData[9];
				currentValue_B = ackData[10];
				currentValue_W = ackData[11];
				updateCurrentValue();
			}
			else
			{
				string msg = "电流参数读取错误";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.7");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		// 电流配置 - 普通 - 保存
		private void saveCurrentButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 检查特定参数
			if (checkCurrentPara() == false)
			{
				return;
			}

			// 收集特定参数 - 当前的 currentValue_R/G/B/W
			byte[] currentPara = new byte[4];
			currentPara[0] = (byte)currentValue_R;
			currentPara[1] = (byte)currentValue_G;
			currentPara[2] = (byte)currentValue_B;
			currentPara[3] = (byte)currentValue_W;

			byte[] cmdData = new byte[256];

			cmdData[2] = 0x32;

			for (int i = 0; i < 4; i++)
			{
				cmdData[8 + i] = currentPara[i];
			}
            baseCoreNet.setMCUConfigEnable(subIndex, portIndex, miniIndex);
			
            baseCoreNet.sendMiniCmdWithCheck(devIndex, subIndex, portIndex, miniIndex, cmdData, 32);

			if (enableMessageCheckBox.Checked)
			{
				string msg = "电流配置保存完成";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.8");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		// 电流配置 - 普通 - 整体调整
		private void changeByCurrentFactorButton_Click(object sender, EventArgs e)
		{
			// 检查特定参数
			if (checkCurrentPara() == false)
			{
				return;
			}

			int factor = 0;
			bool parseRes = int.TryParse(totalCurrentFactorTextBox.Text.Trim(), out factor);
			if (parseRes == false || factor < 0 || factor > 200)
			{
				string msg = "调整因子解析错误, 应为 [0,200] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.9");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}
			else
			{
				currentValue_R = currentValue_R * factor / 100;
				currentValue_G = currentValue_G * factor / 100;
				currentValue_B = currentValue_B * factor / 100;
				currentValue_W = currentValue_W * factor / 100;

				currentValue_R = currentValue_R > 100 ? 100 : currentValue_R;
				currentValue_G = currentValue_G > 100 ? 100 : currentValue_G;
				currentValue_B = currentValue_B > 100 ? 100 : currentValue_B;
				currentValue_W = currentValue_W > 100 ? 100 : currentValue_W;

				updateCurrentValue();
			}
		}

		// 电流配置 - 普通 - 设为最大
		private void setMaxCurrentButton_Click(object sender, EventArgs e)
		{
			currentValue_R = 100;
			currentValue_G = 100;
			currentValue_B = 100;
			currentValue_W = 100;
			
			updateCurrentValue();
		}

		// 电流设置 - 高级
		private void currentProButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			Form_DmxCurrentPro currentProForm = new Form_DmxCurrentPro(baseCoreNet);
			currentProForm.setParentParameters(devIndex, subIndex, portIndex, miniIndex);
			parent.addForm("Form_DmxCurrentPro", currentProForm);
			currentProForm.setLanguageText();
			currentProForm.ShowDialog();
			parent.removeForm("Form_DmxCurrentPro");
		}

		// 保存颜色模式
		private void saveColorModeButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 防止重入
			if (dmxChannelSetting == true)
			{
				return;
			}
			else
			{
				dmxChannelSetting = true;
			}

			// 收集特定参数
			int tmpMode = -1;
			bool parseRes = int.TryParse(dmxChannleTextBox.Text.Trim(), out tmpMode);
			if (parseRes == false || tmpMode < 1 || tmpMode > 1024)
			{
				string msg = "DMX通道数解析错误, 应为 [1,1024] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.10");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				dmxChannelSetting = false;
	
				return;
			}
			else
			{
				dmxChannelNum = tmpMode;
			}

			// 获取解码器控制权
			//baseCoreNet.setControlState(subIndex, true, false);
			//Thread.Sleep(100);

			// 逻辑动作
			baseCoreNet.setMcuColor(subIndex, portIndex, miniIndex, dmxChannelNum);

			//兼容老解码板
			if (oldModeCheckBox.Checked == true)
			{
				// 写DMX1074解码板通道值
				Thread.Sleep(100);
				baseCoreNet.setMcuChannle_old(subIndex, portIndex, miniIndex, (byte)dmxChannelNum);
				
				// 重复位
				Thread.Sleep(100);
				baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
			}

			// 解除解码器控制权
			//Thread.Sleep(100);
			//baseCoreNet.setControlState(subIndex, false, false);

			if (enableMessageCheckBox.Checked)
			{
				string msg = "DMX通道数配置成功";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.11");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

			dmxChannelSetting = false;


		}

		// 选择内控模式
		private void innerControlModeComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			// 常亮模式
			if (innerControlModeComboBox.SelectedIndex == 0)
			{
				brightLabel1.Enabled = true;
				brightLabel2.Enabled = true;
				brightTextBox.Enabled = true;
				brightTrackBar.Enabled = true;
				brightRLabel1.Enabled = true;
				brightRLabel2.Enabled = true;
				brightRTextBox.Enabled = true;
				brightRTrackBar.Enabled = true;
				brightGLabel1.Enabled = true;
				brightGLabel2.Enabled = true;
				brightGTextBox.Enabled = true;
				brightGTrackBar.Enabled = true;
				brightBLabel1.Enabled = true;
				brightBLabel2.Enabled = true;
				brightBTextBox.Enabled = true;
				brightBTrackBar.Enabled = true;
				brightWLabel1.Enabled = true;
				brightWLabel2.Enabled = true;
				brightWTextBox.Enabled = true;
				brightWTrackBar.Enabled = true;
				hLabel1.Enabled = true;
				hLabel2.Enabled = true;
				hLabel3.Enabled = true;
				hLabel4.Enabled = true;

				speedLabel1.Enabled = false;
				speedLabel2.Enabled = false;
				speedLabel3.Enabled = false;
				speedLabel4.Enabled = false;
				speedTextBox.Enabled = false;
				speedTrackBar.Enabled = false;
			}
			// 跳变模式、渐变模式
			else if (innerControlModeComboBox.SelectedIndex == 1 || innerControlModeComboBox.SelectedIndex == 2)
			{
				brightLabel1.Enabled = true;
				brightLabel2.Enabled = true;
				brightTextBox.Enabled = true;
				brightTrackBar.Enabled = true;
				brightRLabel1.Enabled = true;
				brightRLabel2.Enabled = true;
				brightRTextBox.Enabled = true;
				brightRTrackBar.Enabled = true;
				brightGLabel1.Enabled = true;
				brightGLabel2.Enabled = true;
				brightGTextBox.Enabled = true;
				brightGTrackBar.Enabled = true;
				brightBLabel1.Enabled = true;
				brightBLabel2.Enabled = true;
				brightBTextBox.Enabled = true;
				brightBTrackBar.Enabled = true;
				brightWLabel1.Enabled = true;
				brightWLabel2.Enabled = true;
				brightWTextBox.Enabled = true;
				brightWTrackBar.Enabled = true;
				hLabel1.Enabled = true;
				hLabel2.Enabled = true;
				hLabel3.Enabled = true;
				hLabel4.Enabled = true;

				speedLabel1.Enabled = true;
				speedLabel2.Enabled = true;
				speedLabel3.Enabled = true;
				speedLabel4.Enabled = true;
				speedTextBox.Enabled = true;
				speedTrackBar.Enabled = true;
			}
			// 无自检
			if (innerControlModeComboBox.SelectedIndex == 3)
			{
				brightLabel1.Enabled = false;
				brightLabel2.Enabled = false;
				brightTextBox.Enabled = false;
				brightTrackBar.Enabled = false;
				brightRLabel1.Enabled = false;
				brightRLabel2.Enabled = false;
				brightRTextBox.Enabled = false;
				brightRTrackBar.Enabled = false;
				brightGLabel1.Enabled = false;
				brightGLabel2.Enabled = false;
				brightGTextBox.Enabled = false;
				brightGTrackBar.Enabled = false;
				brightBLabel1.Enabled = false;
				brightBLabel2.Enabled = false;
				brightBTextBox.Enabled = false;
				brightBTrackBar.Enabled = false;
				brightWLabel1.Enabled = false;
				brightWLabel2.Enabled = false;
				brightWTextBox.Enabled = false;
				brightWTrackBar.Enabled = false;
				hLabel1.Enabled = false;
				hLabel2.Enabled = false;
				hLabel3.Enabled = false;
				hLabel4.Enabled = false;

				speedLabel1.Enabled = false;
				speedLabel2.Enabled = false;
				speedLabel3.Enabled = false;
				speedLabel4.Enabled = false;
				speedTextBox.Enabled = false;
				speedTrackBar.Enabled = false;
			}
		}

		// 调整整体亮度值
		private void brightTrackBar_Scroll(object sender, EventArgs e)
		{
			brightness = brightTrackBar.Value;
			brightTextBox.Text = brightness.ToString();

			brightnessR = brightTrackBar.Value;
			brightnessG = brightTrackBar.Value;
			brightnessB = brightTrackBar.Value;
			brightnessW = brightTrackBar.Value;
			brightRTextBox.Text = brightnessR.ToString();
			brightGTextBox.Text = brightnessG.ToString();
			brightBTextBox.Text = brightnessB.ToString();
			brightWTextBox.Text = brightnessW.ToString();
		}

		// 调整分色亮度值 - R
		private void brightRTrackBar_Scroll(object sender, EventArgs e)
		{
			brightnessR = brightRTrackBar.Value;
			brightRTextBox.Text = brightnessR.ToString();
		}

		// 调整分色亮度值 - G
		private void brightGTrackBar_Scroll(object sender, EventArgs e)
		{
			brightnessG = brightGTrackBar.Value;
			brightGTextBox.Text = brightnessG.ToString();
		}

		// 调整分色亮度值 - B
		private void brightBTrackBar_Scroll(object sender, EventArgs e)
		{
			brightnessB = brightBTrackBar.Value;
			brightBTextBox.Text = brightnessB.ToString();
		}

		// 调整分色亮度值 - W
		private void brightWTrackBar_Scroll(object sender, EventArgs e)
		{
			brightnessW = brightWTrackBar.Value;
			brightWTextBox.Text = brightnessW.ToString();
		}

		// 调整速度值
		private void speedTrackBar_Scroll(object sender, EventArgs e)
		{
			speed = speedTrackBar.Value;
			speedTextBox.Text = speed.ToString();
		}

		// 手动填写整体亮度值
		private void brightTextBox_TextChanged(object sender, EventArgs e)
		{
			if (brightTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpBright = -1;
			bool parseRes = int.TryParse(brightTextBox.Text.Trim(), out tmpBright);
			if (parseRes == false || tmpBright < 0 || tmpBright > 100)
			{
				return;
			}
			else
			{
				brightness = tmpBright;
				brightTrackBar.Value = tmpBright;

				brightnessR = tmpBright;
				brightnessG = tmpBright;
				brightnessB = tmpBright;
				brightnessW = tmpBright;
				brightRTextBox.Text = brightnessR.ToString();
				brightGTextBox.Text = brightnessG.ToString();
				brightBTextBox.Text = brightnessB.ToString();
				brightWTextBox.Text = brightnessW.ToString();
				brightRTrackBar.Value = brightnessR;
				brightGTrackBar.Value = brightnessG;
				brightBTrackBar.Value = brightnessB;
				brightWTrackBar.Value = brightnessW;
			}
		}

		// 手动填写分色亮度值 - R
		private void brightRTextBox_TextChanged(object sender, EventArgs e)
		{
			if (brightRTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpBright = -1;
			bool parseRes = int.TryParse(brightRTextBox.Text.Trim(), out tmpBright);
			if (parseRes == false || tmpBright < 0 || tmpBright > 100)
			{
				return;
			}
			else
			{
				brightnessR = tmpBright;
				brightRTrackBar.Value = tmpBright;
			}
		}

		// 手动填写分色亮度值 - G
		private void brightGTextBox_TextChanged(object sender, EventArgs e)
		{
			if (brightGTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpBright = -1;
			bool parseRes = int.TryParse(brightGTextBox.Text.Trim(), out tmpBright);
			if (parseRes == false || tmpBright < 0 || tmpBright > 100)
			{
				return;
			}
			else
			{
				brightnessG = tmpBright;
				brightGTrackBar.Value = tmpBright;
			}
		}

		// 手动填写分色亮度值 - B
		private void brightBTextBox_TextChanged(object sender, EventArgs e)
		{
			if (brightBTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpBright = -1;
			bool parseRes = int.TryParse(brightBTextBox.Text.Trim(), out tmpBright);
			if (parseRes == false || tmpBright < 0 || tmpBright > 100)
			{
				return;
			}
			else
			{
				brightnessB = tmpBright;
				brightBTrackBar.Value = tmpBright;
			}
		}

		// 手动填写分色亮度值 - W
		private void brightWTextBox_TextChanged(object sender, EventArgs e)
		{
			if (brightWTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpBright = -1;
			bool parseRes = int.TryParse(brightWTextBox.Text.Trim(), out tmpBright);
			if (parseRes == false || tmpBright < 0 || tmpBright > 100)
			{
				return;
			}
			else
			{
				brightnessW = tmpBright;
				brightWTrackBar.Value = tmpBright;
			}
		}

		// 手动填写速度值
		private void speedTextBox_TextChanged(object sender, EventArgs e)
		{
			if (speedTextBox.Text.Trim().Equals(""))
			{
				return;
			}

			int tmpSpeed = -1;
			bool parseRes = int.TryParse(speedTextBox.Text.Trim(), out tmpSpeed);
			if (parseRes == false || tmpSpeed < 1 || tmpSpeed > 100)
			{
				return;
			}
			else
			{
				speed = tmpSpeed;
				speedTrackBar.Value = tmpSpeed;
			}
		}

		// 保存内控模式
		private void saveInnerControlButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 防止重入
			if (innerControlSetting == true)
			{
				return;
			}
			else
			{
				innerControlSetting = true;
			}

			// 校验整体亮度参数
			if (innerControlModeComboBox.SelectedIndex != 3)
			{
				int tmpBright = -1;
				bool parseRes1 = int.TryParse(brightTextBox.Text.Trim(), out tmpBright);
				if (parseRes1 == false || tmpBright < 0 || tmpBright > 100)
				{
					string msg = "亮度值解析错误, 应为 [0,100] 之间的整数";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.12");
					}
					
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					innerControlSetting = false;
					
					return;
				}
				else
				{
					brightness = tmpBright;
					brightTrackBar.Value = tmpBright;
				}
			}
			// 校验速度参数
			if (innerControlModeComboBox.SelectedIndex != 0)
			{
				int tmpSpeed = -1;
				bool parseRes2 = int.TryParse(speedTextBox.Text.Trim(), out tmpSpeed);
				if (parseRes2 == false || tmpSpeed < 1 || tmpSpeed > 100)
				{
					string msg = "速度值解析错误, 应为 [1,100] 之间的整数";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.13");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					innerControlSetting = false;
					
					return;
				}
				else
				{
					speed = tmpSpeed;
					speedTrackBar.Value = tmpSpeed;
				}
			}
			// 校验分色亮度参数
			if (innerControlModeComboBox.SelectedIndex != 3)
			{
				// 分色亮度 - R
				int tmpBrightR = -1;
				bool parseResR = int.TryParse(brightRTextBox.Text.Trim(), out tmpBrightR);
				if (parseResR == false || tmpBrightR < 0 || tmpBrightR > 100)
				{
					string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "R");
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "R");
					}
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					innerControlSetting = false;
					return;
				}
				else
				{
					brightnessR = tmpBrightR;
					brightRTrackBar.Value = tmpBrightR;
				}

				// 分色亮度 - G
				int tmpBrightG = -1;
				bool parseResG = int.TryParse(brightGTextBox.Text.Trim(), out tmpBrightG);
				if (parseResG == false || tmpBrightG < 0 || tmpBrightG > 100)
				{
					string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "G");
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "G");
					}
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					innerControlSetting = false;
					return;
				}
				else
				{
					brightnessG = tmpBrightG;
					brightGTrackBar.Value = tmpBrightG;
				}

				// 分色亮度 - B
				int tmpBrightB = -1;
				bool parseResB = int.TryParse(brightBTextBox.Text.Trim(), out tmpBrightB);
				if (parseResB == false || tmpBrightB < 0 || tmpBrightB > 100)
				{
					string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "B");
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "B");
					}
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					innerControlSetting = false;
					return;
				}
				else
				{
					brightnessB = tmpBrightB;
					brightBTrackBar.Value = tmpBrightB;
				}

				// 分色亮度 - W
				int tmpBrightW = -1;
				bool parseResW = int.TryParse(brightWTextBox.Text.Trim(), out tmpBrightW);
				if (parseResW == false || tmpBrightW < 0 || tmpBrightW > 100)
				{
					string msg = string.Format("电流参数 {0} 解析错误, 应为 [0,100] 之间的整数", "W");
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.4"), "W");
					}
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					innerControlSetting = false;
					return;
				}
				else
				{
					brightnessW = tmpBrightW;
					brightWTrackBar.Value = tmpBrightW;
				}
			}

            // 收集内控模式参数: 0-常量模式，1-跳变模式，2-渐变模式，3-无自检， 4-定制模式
			int innerMode = innerControlModeComboBox.SelectedIndex;

			// 收集整体亮度参数并转换, 收集速度参数
            if (innerMode == 0) {		    // 常亮模式 
                brightValue = (int)(brightness / 100.0 * 65535);
                speedValue = 0;
            } else if (innerMode == 1) {    // 跳变模式 
                brightValue = (int)(brightness / 100.0 * 65535);
                speedValue = speed;
            } else if (innerMode == 2) {	// 渐变模式 
                brightValue = (int)(brightness / 100.0 * 65535);
                speedValue = speed;
            } else if (innerMode == 3) {    // 无自检 
                brightValue = 0;
                speedValue = 0;
            } else if (innerMode >= 4) {	// 定制模式
                brightValue = (int)(brightness / 100.0 * 65535);
                speedValue = speed;
            }

			// 收集分色亮度参数
			brightValueR = (int)(brightnessR / 100.0 * 255);
			brightValueG = (int)(brightnessG / 100.0 * 255);
			brightValueB = (int)(brightnessB / 100.0 * 255);
			brightValueW = (int)(brightnessW / 100.0 * 255);
			int[] rgbw = new int[] { brightValueR, brightValueG, brightValueB, brightValueW };

			// 收集无信号标志位: 0-最后一帧，1-内控模式，2-黑灯
			int noSignalMode = noSignalComboBox.SelectedIndex;

			// 逻辑动作
			baseCoreNet.setMcuInnorControlMode(subIndex, portIndex, miniIndex, brightValue, speedValue, innerMode, noSignalMode, rgbw);

			/*
			if (innerMode == 0)			// 常亮模式
			{
				baseCoreNet.setMcuLight(subIndex, portIndex, miniIndex, brightValue);
			}
			else if (innerMode == 1)	// 跳变模式
			{
				baseCoreNet.setMcuInnorControlMode(subIndex, portIndex, miniIndex, brightValue, speed, 1);
			}
			else if (innerMode == 2)	// 渐变模式
			{
				baseCoreNet.setMcuInnorControlMode(subIndex, portIndex, miniIndex, brightValue, speed, 2);
			}
			else if (innerMode == 3)	// 无自检
			{
				baseCoreNet.setMcuInnorControlMode(subIndex, portIndex, miniIndex, brightValue, speedValue, innerMode, noSignalMode, rgbw);
			}
			*/

			if (enableMessageCheckBox.Checked)
			{
				string msg = "内控模式配置成功";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.14");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

			innerControlSetting = false;
		}

		// 选择对比度
		private void grayLevelComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			int grayLevelIndex, pwmFreq;
			grayLevelIndex = grayLevelComboBox.SelectedIndex;
			//if (grayLevelIndex > 4)
			//{
			//	grayLevelIndex = 4;
			//}
			pwmFreq = 1 << grayLevelIndex;
			pwmFreq = pwmFreq * 240;
			pwmFreqTextBox.Text = pwmFreq.ToString() + "Hz";
		}

		// 保存 PWM 曲线参数
		private void savePwmButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 防止重入
			if (pwmSetting == true)
			{
				return;
			}
			else
			{
				pwmSetting = true;
			}

			try
			{
				int grayLevelIndex;
				grayLevelIndex = grayLevelComboBox.SelectedIndex;
				//if (grayLevelIndex > 4)
				//{
				//	grayLevelIndex = 4;
				//}

				pwmContrast = 65535;
				pwmLevel = 16 - grayLevelIndex;
				pwmGamma = Convert.ToDouble(pwmGammaTextBox.Text.Trim());
				startGray = Convert.ToInt32(startGrayTextBox.Text.Trim());
				startPWM = Convert.ToInt32(startPWMTextBox.Text.Trim());
				effectLevel = effectLevelComoBox.SelectedIndex + 1;

				string msg1 = "PWM曲线参数配置：对比度范围应为 [0,65535]";
				string msg2 = "PWM曲线参数配置：伽马值范围应为 [1.0,3.0]";
				string msg3 = "PWM曲线参数配置：起灰灰度范围应为 [1,50]";
				string msg4 = "PWM曲线参数配置：起灰PWM值范围应为 [1,200]";
				if (LanguageOper.available == true)
				{
					msg1 = LanguageOper.ReadValue(section, "msgStr.15");
					msg2 = LanguageOper.ReadValue(section, "msgStr.16");
					msg3 = LanguageOper.ReadValue(section, "msgStr.17");
					msg4 = LanguageOper.ReadValue(section, "msgStr.18");
				}

				if (pwmContrast > 65535 || pwmContrast < 0)
				{
					MessageBoxEx.Show(this, msg1, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					pwmSetting = false;
					return;
				}
				if (pwmGamma > 3.0 || pwmGamma < 1.0)
				{
					MessageBoxEx.Show(this, msg2, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					pwmSetting = false;
					return;
				}
				if (startGray > 50 || startGray < 1)
				{
					MessageBoxEx.Show(this, msg3, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					pwmSetting = false;
					return;
				}
				if (startPWM > 200 || startPWM < 1)
				{
					MessageBoxEx.Show(this, msg4, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					pwmSetting = false;
					return;
				}

				// 参数值内部处理
				pwmContrast = pwmContrast >> 2;
				pwmGamma = pwmGamma * 10;
			}
			catch (Exception)
			{
				string msg = "PWM曲线参数配置：参数解析错误, 请检查";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.19");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Error);
				
				pwmSetting = false;
				
				return;
			}

			// 逻辑动作
			baseCoreNet.setMcuPWM(subIndex, portIndex, miniIndex, pwmContrast, (int)pwmGamma, startPWM, pwmLevel, startGray, effectLevel);

			if (enableMessageCheckBox.Checked)
			{
				string msg = "PWM曲线参数配置成功";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.20");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

			pwmSetting = false;
		}

		// 关闭窗体
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		// 配置初始化
		private void dmxSpiListButton_Click(object sender, EventArgs e)
		{
			/*OpenFileDialog o = new OpenFileDialog();
			o.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			o.Multiselect = false;
			o.Filter = "bin文件(*.bin)|*.bin";
			if (o.ShowDialog() == DialogResult.OK)
			{
				FileStream fs = new FileStream(o.FileName, FileMode.Open);
				BinaryReader reader = new BinaryReader(fs);
				if (fs.Length == 0)
				{
					string msg = "文件错误";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.21");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
					
					return;
				}

				// 读取文件内容
				int dataLength = (int)fs.Length;
				byte[] binBuff = new byte[dataLength];
				fs.Read(binBuff, 0, dataLength);
				fs.Close();

				int i,lll;
				byte[] configBuff = new byte[128];

				// 配置数据初始化
				for (i = 0; i < 128; i++)
				{
					configBuff[i] = 0;
				}

				if (dataLength > 64)
				{
					lll = 64;
				}
				else
				{
					lll = dataLength;
				}

				// 配置数据填充
				for (i = 0; i < lll; i++)
				{
					configBuff[i] = binBuff[i];
					configBuff[i + 64] = (byte)(255 - configBuff[i]);
				}

				// 获取解码器控制权
				baseCoreNet.setControlState(subIndex, true, false);
				Thread.Sleep(100);

				// 设置升级模式
				baseCoreNet.setMcuInit(subIndex, portIndex, miniIndex);
				Thread.Sleep(100);

				// 写入调整数据
				baseCoreNet.setN76E003Write(subIndex, portIndex, miniIndex, 0x3700, configBuff);
				Thread.Sleep(50);

				// 设置工作模式
				baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
				Thread.Sleep(100);

				// 解除解码器控制权
				baseCoreNet.setControlState(subIndex, false, false);
				Thread.Sleep(100);

				string msg1 = "配置成功";
				if (LanguageOper.available == true)
				{
					msg1 = LanguageOper.ReadValue(section, "msgStr.22");
				}

				MessageBoxEx.Show(this, msg1, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}*/
            // 读取配置文件
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Bin Files(*.bin)|*.bin";
            o.Multiselect = false;
            if (Directory.Exists(parent.lastPath_ReceiverControl_UpdateSrcFile))
            {
                o.InitialDirectory = parent.lastPath_ReceiverControl_UpdateSrcFile;
            }
            else
            {
                o.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            }
            DialogResult dr = o.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string tmpSrcPath = o.FileName;
                FileStream fs = new FileStream(tmpSrcPath, FileMode.Open);
                BinaryReader reader = new BinaryReader(fs);

                int dataLength = (int)fs.Length;
                byte[] dataBuff = new byte[65536];
                reader.Read(dataBuff, 0, dataLength);
                reader.Close();
                if (dataLength > 64)
                {
                    dataLength = 64;
                }
                // 获取解码器控制权
                baseCoreNet.setControlState(subIndex, true, false);
                Thread.Sleep(100);

                // 写入调整数据
                int maxlength = 0x20;
                byte[] tempdata = new byte[maxlength];
                for (int i = 0; i < dataLength; i += maxlength)
                {
                    for (int k = 0; k < maxlength; k++)
                    {
                        tempdata[k] = dataBuff[i + k];
                    }
                    baseCoreNet.setMcuConfig(subIndex, portIndex, miniIndex, i, tempdata);
                    Thread.Sleep(100);
                }

                // 设置工作模式
                baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
                Thread.Sleep(100);

                // 解除解码器控制权
                baseCoreNet.setControlState(subIndex, false, false);
                Thread.Sleep(100);
                
            }

            
		}
        private void dmxReadCfgButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog dig = new SaveFileDialog();
            dig.Filter = "Bin Files(*.bin)|*.bin";
            if (Directory.Exists(parent.lastPath_ReceiverControl_UpdateSrcFile))
            {
                dig.InitialDirectory = parent.lastPath_ReceiverControl_UpdateSrcFile;
            }
            else
            {
                dig.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            }
            DialogResult dr = dig.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string tmpSrcPath = dig.FileName;
                FileStream fs = new FileStream(tmpSrcPath, FileMode.OpenOrCreate);
                BinaryWriter writer = new BinaryWriter(fs);

                int dataLength = 64;
                byte[] dataBuff = new byte[65536];
                // 获取解码器控制权
                baseCoreNet.setControlState(subIndex, true, false);
                Thread.Sleep(100);

                // 读取调整数据
                int maxlength = 0x20;
                byte[] tempdata = null;
                for (int i = 0; i < dataLength; i += maxlength)
                {
                    tempdata = baseCoreNet.getMCUConfig(subIndex, portIndex, miniIndex, i);
                    Thread.Sleep(100);
                    for (int k = 0; k < maxlength; k++)
                    {
                        dataBuff[i * maxlength + k] = tempdata[k];
                    }
                }

                writer.Write(dataBuff, 0, 64);
                writer.Close();

                // 设置工作模式
                baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
                Thread.Sleep(100);

                // 解除解码器控制权
                baseCoreNet.setControlState(subIndex, false, false);
                Thread.Sleep(100);

            }
        }

		// 输入输出映射
		private void lMapButton_Click(object sender, EventArgs e)
		{
			OpenFileDialog o = new OpenFileDialog();
			o.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
			o.Multiselect = false;
			o.Filter = "bin文件(*.bin)|*.bin";
			if (o.ShowDialog() == DialogResult.OK)
			{
				FileStream fs = new FileStream(o.FileName, FileMode.Open);
				BinaryReader reader = new BinaryReader(fs);
				if (fs.Length == 0)
				{
					return;
				}

				// 读取文件内容
				int dataLength = (int)fs.Length;
				byte[] binBuff = new byte[dataLength];
				fs.Read(binBuff, 0, dataLength);
				fs.Close();

				int i, lll;
				byte[] configBuff = new byte[128];

				// 配置数据初始化
				for (i = 0; i < 128; i++)
				{
					configBuff[i] = 0;
				}

				if (dataLength > 128)
				{
					lll = 128;
				}
				else
				{
					lll = dataLength;
				}

				// 配置数据填充
				for (i = 0; i < lll; i++)
				{
					configBuff[i] = binBuff[i];
				}

				// 获取解码器控制权
				baseCoreNet.setControlState(subIndex, true, false);
				Thread.Sleep(100);

				// 设置升级模式
				baseCoreNet.setMcuInit(subIndex, portIndex, miniIndex);
				Thread.Sleep(100);

				// 写入调整数据
				baseCoreNet.setN76E003Write(subIndex, portIndex, miniIndex, 0x3500, configBuff);
				Thread.Sleep(50);

				// 校验数据生成
				for (i = 0; i < 128; i++)
				{
					configBuff[i] = (byte)(255 - configBuff[i]);
				}
				
				// 写入调整数据校验
				baseCoreNet.setN76E003Write(subIndex, portIndex, miniIndex, 0x3580, configBuff);
				Thread.Sleep(50);

				//设置工作模式
				baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
				Thread.Sleep(100);

				// 解除解码器控制权
				baseCoreNet.setControlState(subIndex, false, false);
				Thread.Sleep(100);

				string msg = "配置成功";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.22");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		// 输入输出映射使能
		private void dmxMapCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			inOutMapButton.Enabled = dmxMapCheckBox.Checked;
		}

		// 全功率配置
		private void fullPowerButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
            if (getCommonParameter() == false)
            {
                return;
            }

			Form_FullPower fullPowerForm = new Form_FullPower(baseCoreNet, parent, devIndex, subIndex, portIndex, miniIndex);
			parent.addForm("Form_FullPower", fullPowerForm);
			fullPowerForm.setLanguageText();
			fullPowerForm.ShowDialog();
			parent.removeForm("Form_FullPower");
		}

		private void saveChannleButton_Click(object sender, EventArgs e)
		{
			// 收集通用参数
			if (getCommonParameter() == false)
			{
				return;
			}

			// 防止重入
			if (dmxChannelSetting == true)
			{
				return;
			}
			else
			{
				dmxChannelSetting = true;
			}

			// 收集特定参数
			int tmpMode = -1;
			bool parseRes = int.TryParse(dmxChannleTextBox.Text.Trim(), out tmpMode);
			if (parseRes == false || tmpMode < 1 || tmpMode > 512)
			{
				string msg = "DMX通道数解析错误, 应为 [1,512] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.10");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

				dmxChannelSetting = false;
	
				return;
			}
			else
			{
				dmxChannelNum = tmpMode;
			}

			// 获取解码器控制权
			//baseCoreNet.setControlState(subIndex, true, false);
			//Thread.Sleep(100);

			// 逻辑动作
			//baseCoreNet.setMcuColor(subIndex, portIndex, miniIndex, (byte)dmxChannelNum);

			//兼容老解码板
			//if (oldModeCheckBox.Checked == true)
			//{
				//Thread.Sleep(30);
				baseCoreNet.setMcuChannle_old(subIndex, portIndex, miniIndex, (byte)dmxChannelNum);
			//}

			// 解除解码器控制权
			//baseCoreNet.setControlState(subIndex, false, false);
			//Thread.Sleep(100);

			if (enableMessageCheckBox.Checked)
			{
				string msg = "DMX通道数配置成功";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.11");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}

			dmxChannelSetting = false;
		}

        private void TimeLockButton_Click(object sender, EventArgs e)
        {
            Form_TimeLockConfig timeConfigForm = new Form_TimeLockConfig(baseCoreNet, subIndex, portIndex, miniIndex);
            timeConfigForm.setLanguageText();
            timeConfigForm.ShowDialog();

        }

        private void btnColorCfg_Click(object sender, EventArgs e)
        {
            Form_ConvertColor cvtColorForm = new Form_ConvertColor(baseCoreNet, subIndex, portIndex, miniIndex);
            cvtColorForm.ShowDialog();
        }
	}
}
